package cn.edu.jxau.test;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.BitSet;
import java.util.Objects;

/**
 * 海量数据(无重复，非负数)排序
 * 
 * @author Fudashi
 */
public class Main {

	public static void main(String[] args) throws IOException {

		sort(new File("C:\\Users\\Fudashi\\Desktop\\data.txt"));
	}

	public static void sort(File file) throws IOException {

		// 1.参数校验 //
		Objects.requireNonNull(file, "参数file不能为null");
		if (!file.exists()) {
			throw new IOException("文件不存在");
		}
		if (file.isDirectory()) {
			throw new IOException("不能是文件夹");
		}
		if (!file.canRead()) {
			throw new IOException("文件不可读");
		}
		// 排序 //
		BitSet bitSet = new BitSet();
		try (FileInputStream fileIn = new FileInputStream(file); DataInputStream in = new DataInputStream(fileIn)) {
			while (true) {
				bitSet.set(in.readInt());
			}
		} catch (EOFException e) {
			// ignore
		} catch (IOException e) {
			throw new IOException("文件读取失败");
		}
		for (int i = 0, end = bitSet.length(); i < end; i++) {
			if (bitSet.get(i)) {
				System.out.print(i + " ");
			}
		}
	}
}